//
//  shouViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "shouViewController.h"

#import "Masonry.h"

#import "RegViewController.h"

@interface shouViewController ()

@property(nonatomic, retain)UITextField *mytext;
@property(nonatomic, retain)UITextField *youtext;
@property(nonatomic, retain)UIButton *button2;

@end

@implementation shouViewController
-(void)dealloc{
    [_mytext release];
    [_youtext release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:231/255.0 green:229/255.0 blue:230/255.0 alpha:1];
    
    UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
    [self.view addGestureRecognizer:tapgesture];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, view.frame.size.height/2, 30, 20);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [view addSubview:button];
    button.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width/2-30, view.frame.size.height/2, 60, 20)];
    label.text = @"短信验证";
    label.font = [UIFont systemFontOfSize:15];
    label.backgroundColor = [UIColor clearColor];
    [view addSubview:label];
    
//手机号输入栏
    self.mytext = [[UITextField alloc] init];
    self.mytext.placeholder = @"+86 输入手机号";
    self.mytext.layer.borderWidth = 1;
    self.mytext.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mytext];
    [self.mytext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(150);
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.height.mas_equalTo(30);
        
    }];
    
//验证码输入栏
    self.youtext = [[UITextField alloc] init];
    self.youtext.backgroundColor = [UIColor whiteColor];
//边框凹样式
    [self.youtext setBorderStyle:UITextBorderStyleBezel];
    
    [self.view addSubview:self.youtext];
    [self.youtext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mytext.mas_bottom).offset(40);
        make.height.mas_equalTo(_mytext.mas_height);
        make.width.mas_equalTo(70);
        make.left.mas_equalTo(_mytext.mas_left).offset(40);
        
    }];
    
//获取验证码按钮
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.layer.borderWidth = 1;
    self.button2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.button2];
    [self.button2 setTitle:@"获取" forState:UIControlStateNormal];
    self.button2.font = [UIFont systemFontOfSize:15];
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchUpInside];
    [self.button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_youtext.mas_right).offset(20);
        make.top.mas_equalTo(_youtext);
        make.height.mas_equalTo(_youtext);
        make.width.mas_equalTo(60);
    }];

//短信验证按钮
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:button1];
    [button1 setTitle:@"开始验证" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUpInside];
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.button2.mas_bottom).offset(150);
        make.left.mas_equalTo(70);
        make.right.mas_equalTo(-70);
        make.height.mas_equalTo(_youtext);
        
    }];
    
    
    
    // Do any additional setup after loading the view.
}
//返回效果
-(void)button:(UIButton *)button{
[self dismissViewControllerAnimated:YES completion:^{
    
    
}];
}
//键盘回收
-(void)tapGes:(UITapGestureRecognizer *)noti{
    
   [self.mytext resignFirstResponder];
    [self.youtext resignFirstResponder];
}
////获取验证码按钮点击方法
-(void)button2:(UIButton *)button{

        
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.mytext.text zone:@"86" customIdentifier:@"" result:^(NSError *error) {
            if (!error) {
                NSLog(@"获取验证码成功");
                
                 [self openCountdown];
                
            }else{
                NSLog(@"获取验证码失败%@",error);
                if (_mytext.text != nil){
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"😰手机号为空" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                    [self presentViewController:alertController animated:YES completion:nil];
                    UIAlertAction *canAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alertController addAction:canAction];
                }
            }
            
        }];
    

}
    
    
    


// 开启倒计时效果
-(void)openCountdown{
        
        __block NSInteger time = 59; //倒计时时间
        
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        
        dispatch_source_set_event_handler(_timer, ^{
            
            if(time <= 0){ //倒计时结束，关闭
                
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    //设置按钮的样式
                    [self.button2 setTitle:@"重新发送" forState:UIControlStateNormal];
                    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                   self.button2.userInteractionEnabled = YES;
                });
                
            }else{
                
                int seconds = time % 60;
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    //设置按钮显示读秒效果
                    [self.button2 setTitle:[NSString stringWithFormat:@"%.2d秒", seconds] forState:UIControlStateNormal];
                    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    self.button2.userInteractionEnabled = NO;
                });
                time--;
            }
        });
        dispatch_resume(_timer);
}

    

//
//
////短信验证按钮点击方法
-(void)button1:(UIButton *)button{
    [SMSSDK commitVerificationCode:self.youtext.text phoneNumber:self.mytext.text zone:@"86" result:^(NSError *error) {
        
        if (!error) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"username" object:self.mytext.text];
            
            NSLog(@"验证成功");
        }else{
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"😰验证失败" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertController animated:YES completion:nil];
            UIAlertAction *canAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:canAction];
            NSLog(@"验证失败%@",error);
        }
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
